﻿Public Class CategoriaProductos 
    Public Sub Mostrar()
        If guardar.Enabled = False Then
            guardar.Enabled = True
            cancelar.Enabled = True
            agregar.Enabled = False
            modificar.Enabled = False
            eliminar.Enabled = False
            imprimir.Enabled = False
            PanelArriba.Enabled = True
            PanelAbajo.Enabled = False
        Else
            cancelar.Enabled = False
            guardar.Enabled = False
            agregar.Enabled = True
            Modificar.Enabled = True
            Eliminar.Enabled = True
            imprimir.Enabled = True
            PanelArriba.Enabled = False
            PanelAbajo.Enabled = True
        End If
    End Sub

    Private Sub ProductoscategoriaBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProductoscategoriaBindingNavigatorSaveItem.Click
        Try
            Me.Validate()
            Me.ProductoscategoriaBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)

        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudo almacenar los datos")
        End Try
    End Sub

    Private Sub CategoriaProductos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productoscategoria' table. You can move, or remove it, as needed.
        Try
            Me.ProductoscategoriaTableAdapter.Fill(Me.DbFireAndIceDataSet.productoscategoria)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar los datos de Categoria Productos")
        End Try
        PanelArriba.Enabled = False
    End Sub

    Private Sub agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles agregar.Click
        BindingNavigatorAddNewItem.PerformClick()
        Mostrar()
        IdproductoscategoriaTextBox.ReadOnly = True
        DescripcionTextBox.ReadOnly = False
        ObservacionTextBox.ReadOnly = False
    End Sub

    Private Sub modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles modificar.Click
        Mostrar()
        DescripcionTextBox.ReadOnly = False
        ObservacionTextBox.ReadOnly = False
    End Sub

    Private Sub eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eliminar.Click
        Dim opcion As Integer
        opcion = MsgBox("¿Está seguro que desea eliminar el registro?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Eliminar registro")
        If opcion = vbYes Then
            Try
                'ProductoscategoriaTableAdapter.DeleteQueryProcat(IdproductoscategoriaTextBox.Text)
                'Me.ProductoscategoriaTableAdapter.Fill(Me.DbFireAndIceDataSet.productoscategoria)
                AlertControl1.Show(Owner, "Categorías de Productos", "Se eliminaron los datos")
                Me.ProductoscategoriaTableAdapter.Fill(Me.DbFireAndIceDataSet.productoscategoria)
            Catch ex As Exception
                AlertControl1.AppearanceCaption.ForeColor = Color.Red
                AlertControl1.Show(Owner, "ERROR", "Lo sentimos, el registro no puede ser eliminado ya que está relacionado con otras tablas")
            End Try
        End If

    End Sub

    Private Sub guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles guardar.Click
        If DescripcionTextBox.Text.Length = 0 Then
            ErrorProvider1.SetError(DescripcionTextBox, "Debe ingresar una descripcion del producto")
        Else
            ErrorProvider1.SetError(DescripcionTextBox, Nothing)
            ProductoscategoriaBindingNavigatorSaveItem.PerformClick()
            AlertControl1.AppearanceCaption.ForeColor = Color.Green
            AlertControl1.Show(Owner, "Categoría Producto", "Se han almacenado los datos correctamente")
            Mostrar()
            IdproductoscategoriaTextBox.ReadOnly = True
            DescripcionTextBox.ReadOnly = True
            ObservacionTextBox.ReadOnly = True
        End If

    End Sub

    Private Sub cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelar.Click
        ErrorProvider1.SetError(DescripcionTextBox, Nothing)
        Mostrar()
        Try
            Me.ProductoscategoriaTableAdapter.Fill(Me.DbFireAndIceDataSet.productoscategoria)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar los datos de Categoria Productos")
        End Try
        
    End Sub

    Private Sub imprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imprimir.Click
        GridControl1.ShowPrintPreview()
    End Sub

End Class